package com.tangbao.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @Discription
 * @Author tangbao
 * @CreateDate 2018/1/14 17:30
 * @Version 1.0
 */
public class DateUtil{

    /**
     * 按照指定格式将字符串类型转为date类
     * @param time 字符串时间
     * @param format 格式化格式
     * @return
     */
    public static Date parseStrToDate(String time,String format) {
        Date date = null;
        SimpleDateFormat ymd = new SimpleDateFormat(format);
        try {
            date = ymd.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 把Date类型日期按照指定格式转换成字符串
     * @param date 要转换的date
     * @param format 要转换成的格式
     * @return
     */
    public static String formatDateToStr(Date date,String format) {
        String dateTime = null;
        SimpleDateFormat ymd = new SimpleDateFormat(format);
        try {
            dateTime = ymd.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateTime;
    }

    /**
     * 把Timestamp类型日期按照指定格式转换成字符串
     * @param date
     * @param format
     * @return
     */
    public static String formatTimestampToStr(Timestamp date,String format) {
        String dateTime = null;
        SimpleDateFormat ymd = new SimpleDateFormat(format);
        try {
            dateTime = ymd.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateTime;
    }

    /**
     * 根据指定格式获取当前时间
     * @param format
     * @return
     */
    public static String getCurrentTime(String format) {
        String dateStr = null;
        SimpleDateFormat ymd = new SimpleDateFormat(format);
        dateStr = ymd.format(new Date());
        return dateStr;
    }

    /**
     * 获取当前前一小时的时间
     * @param date
     * @param hour
     * @return
     */
    public static Date getBeforeHour(Date date,int hour) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        int day = cl.get(Calendar.HOUR);
        cl.set(Calendar.HOUR,day+hour);
        return cl.getTime();
    }

    /**
     * 日期加减天数
     * @param smdate 原始日期
     * @param day 加减天数
     * @return
     */
    public static Date dateArithmetic(Date smdate,int day) {
        GregorianCalendar gc = new GregorianCalendar();

        try {
            gc.setTime(smdate);
            gc.add(5,day);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return gc.getTime();
    }

    /**
     * 日期加减天数
     * @param smdate 原始日期
     * @param day 加减天数
     * @return
     */
    public static Timestamp dateArithmeticTimestamp(Timestamp smdate,int day) {
        GregorianCalendar gc = new GregorianCalendar();

        try {
            gc.setTime(smdate);
            gc.add(5,day);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Timestamp(gc.getTimeInMillis());
    }

    /**
     * 求两个日期相差天数
     * @param smdate 开始日期
     * @param bdate 结束日期
     * @return
     */
    public static int daysBetween(String smdate,String bdate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        Long between_days = null;
        try {
            cal.setTime(sdf.parse(smdate));
            long time1 = cal.getTimeInMillis();
            cal.setTime(sdf.parse(bdate));
            long time2 = cal.getTimeInMillis();
            between_days = (time2-time1)/(1000*3600*24);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return between_days.intValue();
    }

    /**
     * 求两个日期相差天数
     * @param smdate 开始日期
     * @param bdate 结束日期
     * @return
     */
    public static int daysBetween(Date smdate,Date bdate) {
        Long between_days = null;

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            smdate = sdf.parse(sdf.format(smdate));
            bdate = sdf.parse(sdf.format(bdate));
            Calendar cal = Calendar.getInstance();
            cal.setTime(smdate);
            long time1 = cal.getTimeInMillis();
            cal.setTime(bdate);
            long time2 = cal.getTimeInMillis();
            between_days = (time2 - time1) / (1000*3600*24);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return between_days.intValue();
    }

}
